/**
 *
 */
package org.insia.smartposse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.form.SkillsForm;
import org.insia.smartposse.manager.FreelanceSkillManager;
import org.insia.smartposse.manager.SkillManager;
import org.insia.smartposse.manager.UserManager;
import org.insia.smartposse.manager.list.FreelanceSkillsListManager;
import org.insia.smartposse.manager.list.SkillMapManager;

/**
 * @author cchalloy
 *
 */
public class SkillsAction extends DispatchAction {

	public ActionForward form(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		req.setAttribute("skillMap", new SkillMapManager().getMap());

		return mapping.findForward("form");
	}

	public ActionForward viewSkills(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		req.setAttribute("viewSkills", true);
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		req.setAttribute("skills", new FreelanceSkillsListManager(user));

		return mapping.findForward("viewSkills");
	}

	public ActionForward addSkill(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		SkillsForm skillForm = (SkillsForm) form;
		SkillManager skillManager = new SkillManager(new Integer(req.getParameter("skill")));
		FreelanceSkillManager frSkillManager = new FreelanceSkillManager();
		frSkillManager.populateSkillForm(user, skillManager, skillForm);
		frSkillManager.save();

		resp.sendRedirect(req.getContextPath() + "/skills.do?cmd=viewSkills");
		return null;
	}

	public ActionForward toUpdateSkill(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		SkillsForm skillForm = (SkillsForm) form;
		Integer idSkill = new Integer(req.getParameter("idSkill"));
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceSkillManager frSkillManager = new FreelanceSkillManager(user.getUser(), idSkill);
		req.setAttribute("skillName", frSkillManager.getSkill().getName());

		if (user.equals(frSkillManager.getFreelance())) {
			skillForm.setSkill(frSkillManager.getSkill().getId());
			skillForm.setGrade(frSkillManager.getGrade());
			skillForm.setComment(frSkillManager.getComment());
			skillForm.setRanking(frSkillManager.getRanking());

			return mapping.findForward("toUpdateSkill");
		} else {
			resp.sendRedirect(req.getContextPath() + "/login.do");
			return null;
		}
	}

	public ActionForward updateSkill(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		SkillsForm skillForm = (SkillsForm) form;
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceSkillManager frSkillManager =
			new FreelanceSkillManager(user.getUser(), skillForm.getSkill());
		frSkillManager.setGrade(skillForm.getGrade());
		frSkillManager.setComment(skillForm.getComment());
		frSkillManager.setRanking(skillForm.getRanking());
		frSkillManager.save();

		resp.sendRedirect(req.getContextPath() + "/skills.do?cmd=viewSkills");
		return null;
	}

	public ActionForward deleteSkill(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		Integer idSkill = new Integer(req.getParameter("idSkill"));
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceSkillManager frSkillManager = new FreelanceSkillManager(user.getUser(), idSkill);
		frSkillManager.delete();

		resp.sendRedirect(req.getContextPath() + "/skills.do?cmd=viewSkills");
		return null;
	}

}
